/**
* File Name: mmap_r.c
* Auther: Jichang
* Create Time: 2023年10月06日 星期五 16时15分50秒
* mmap读，非血缘关系
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/mman.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

/* 结构体写到缓冲区 */
struct student {
	int id;
	char name[256];
	int age;
};

void sys_err(const char *str) {
	perror(str);
	exit(1);
}

int main(int argc, char *argv[]) {
	struct student stu;
	struct student *p;
	int fd;
	fd = open("test_map", O_RDONLY);
	if(fd == -1) {
		sys_err("open error");
	}
	
	p = mmap(NULL, sizeof(stu), PROT_READ, MAP_SHARED, fd, 0);
	if(p == MAP_FAILED) {
		sys_err("mmap error");
	}
	close(fd);

	while(1) {
		printf("id:%d, name:%s, age:%d\n", p->id, p->name, p->age);
		sleep(1);
	}

	munmap(p, sizeof(stu));

	return 0;
}

